﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using FineUI;
using SDLITS.Model;
using SDLITS.Common;
using SDLITS.Web;

namespace EmptyProjectNet20
{
    public partial class login : System.Web.UI.Page
    {
        public static int a = 0;
        public static int b = 0;
        public static int c = 0;
        public static bool GetImage(string filename, byte[] Fingerbuf)
        {
            //if (Finger.MXIsFingerOn() == 1)
            //{
            //    if (Finger.MXReadFingerFromSensor(Fingerbuf) == 1)
            //    {
            //        a = 1;
            //    }
            //    else
            //        return true;
            //    if (Finger.MXDetectFinger(Fingerbuf) == 1)
            //    {
            //        b = 1;
            //    }
            //    else
            //        return true;
            //    if (Finger.MXSaveFingerToFile(Fingerbuf, filename) == 1)
            //    {
            //        c = 1;
            //    }
            //    else
            //        return true;
            //    //if (a == 1 && b == 1 && c == 1)
            //    return false;
            //}
            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Finger.MXIsFingerOn() == 1)
                //{
                //    if (Finger.MXReadFingerFromSensor(Fingerbuf) == 0)
                //        return false;
                //    if (Finger.MXDetectFinger(Fingerbuf) == 0)
                //        return false;
                //    if (Finger.MXSaveFingerToFile(Fingerbuf, filename) == 0)
                //        return false;
                //}



                Session.Clear();
                Session.Abandon();
                txt_UserName.Focus();
            }

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            /*V_LoginUser user = SDDbSession.Default.From<V_LoginUser>().Where(V_LoginUser._.UserID == 17).ToFirst();
            if (InitSession(user))
            {
                Response.Redirect("default.aspx");
            }*/
            try
            {
                string UserName = txt_UserName.Text.Trim();
                string UserPwd = txt_Pwd.Text.Trim();
                if (!CheckValidateCode(txt_yzm.Text))
                {
                    Alert.Show("验证码输入错误！请重新输入！");
                    return;
                }
                V_LoginUser user = SDDbSession.Default.From<V_LoginUser>().Where(V_LoginUser._.UserName == UserName && V_LoginUser._.UserPwd == UserPwd).ToFirst();

                if (user != null)
                {
                    if (user.CompanyStatusID.HasValue && user.CompanyStatusID.Value == 2)
                    {
                        Alert.Show("所在公司已被锁定，请联系市场管理员！");
                        return;
                    }
                    if (user.StatusID.HasValue && user.StatusID.Value == 2)
                    {
                        Alert.Show("用户已被锁定，请联系市场管理员！");
                        return;
                    }
                    if (InitSession(user))
                    {
                        ModelLoginUser modelLoginUser = Session["LoginUser"] as ModelLoginUser;
                        if (modelLoginUser != null)
                        {
                            UpdateLoginTime(modelLoginUser.UserID);
                            Response.Redirect("default.aspx");
                        }
                    }
                }
                Alert.Show("用户名/密码错误，或账号已被冻结！");
            }
            catch (Exception ee)
            {
                Alert.Show("登录出错！" + ee.Message);
            }
        }
        private bool InitSession(V_LoginUser ds)
        {
            try
            {
                ModelLoginUser modelLoginUser = new ModelLoginUser
                {
                    PositionID = ds.PositionID.HasValue ? ds.PositionID.Value : -1,
                    RoomID = ds.RoomID.HasValue ? Convert.ToInt32(ds.RoomID) : -1,
                    CompanyName = string.IsNullOrEmpty(ds.CompanyName) ? "" : ds.CompanyName,
                    CompanyID = ds.CompanyID.HasValue ? Convert.ToInt32(ds.CompanyID) : -1,
                    DepartmentName = string.IsNullOrEmpty(ds.DeptName) ? "" : ds.DeptName,
                    DeptID = ds.DeptID.HasValue ? Convert.ToInt32(ds.DeptID) : -1,
                    MemCardNo = string.IsNullOrEmpty(ds.MemCardNo) ? "" : ds.MemCardNo,
                    MemCardID = ds.MemCardID.HasValue ? Convert.ToInt32(ds.MemCardID) : -1,
                    RoleID = ds.RoleID.HasValue ? Convert.ToInt32(ds.RoleID) : -1,
                    UserID = ds.UserID,
                    LoginTime = ds.LoginTime.HasValue ? ds.LoginTime.ToString() : "",
                    RealName = string.IsNullOrEmpty(ds.RealName) ? "" : ds.RealName,
                    UserName = string.IsNullOrEmpty(ds.UserName) ? "" : ds.UserName,
                };
                Session["LoginUser"] = modelLoginUser;
                Session["IsLogin"] = "True";
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool CheckValidateCode(string code)
        {
            try
            {
                if (code.ToUpper() != Session["CheckCode"].ToString().ToUpper())
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        protected void UpdateLoginTime(int UserID)
        {
            SDDbSession.Default.Update<User>(SDLITS.Model.User._.LoginTime, DateTime.Now, SDLITS.Model.User._.UserID == UserID);
        }
    }
}
